//
//  XRBaseCollectionViewController.m
//  YWBaseFrameWork
//
//  Created by CoderYee on 2017/2/21.
//  Copyright © 2017年 NeiQuan. All rights reserved.
//

#import "XRBaseCollectionViewController.h"
#import "XRCommonBaseCollectionCell.h"
#import "MJrefresh.h"
#import "MJRefreshGifHeader.h"
#import "UIScrollView+MJRefresh.h"
#import "UIScrollView+MJExtension.h"
#import "UIView+MJExtension.h"

#import "MJRefreshNormalHeader.h"
#import "MJRefreshGifHeader.h"

#import "MJRefreshBackNormalFooter.h"
#import "MJRefreshBackGifFooter.h"
#import "MJRefreshAutoNormalFooter.h"
#import "MJRefreshAutoGifFooter.h"
#import "UITableViewPlaceHolderHeader.h"
#import "TableViewNoNetWorkView.h"//无网络
#import "TableViewNoDataView.h"//无数据
#import "AFNetworking.h"
@interface XRBaseCollectionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    BOOL           _isNetWorkConnect;
    NSInteger      _NetWorkStatus;
}
@end

@implementation XRBaseCollectionViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configNetSevices];
}
#pragma mark 添加网络监听
-(void)configNetSevices
{
    [self congigNetWorkReachable];
}
-(void)addNavbarView
{
    [super addNavbarView];
}
-(void)addOwnViews
{
    [super addOwnViews];
    [self  addCollectionView];
    [self  collectionViewRegisterCollectionCell];//注册单元格
}
-(void)addCollectionView
{
    _collectionView=[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    [_collectionView setFrame:CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight-kNavStausHeight-kNavBarHeight)];
    if (_hasTabbar){
        [_collectionView setFrame:CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight-49-kNavStausHeight-kNavBarHeight)];
    }
    [_collectionView setBackgroundColor:kAppBakgroundColor];
    [self.view addSubview:_collectionView];
}
-(void)collectionViewRegisterCollectionCell
{
    [_collectionView registerClass:[XRCommonBaseCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([XRCommonBaseCollectionCell class])];
   [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return   self.dataListArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XRCommonBaseCollectionCell *cell=[self configCollectionView:self.collectionView cellforCollectionCell:[XRCommonBaseCollectionCell class] inexPath:indexPath];
    
    return cell;
    
}
//自己创建cell
-(XRCommonBaseCollectionCell*)configCollectionView:(UICollectionView *)collectionView  cellforCollectionCell:(Class)cellClass  inexPath:(NSIndexPath *)indexPath
{
  //断言设定 抛出异常
  NSAssert([cellClass isSubclassOfClass:[UICollectionViewCell class] ], @"The cellClass  should  be UICollectionViewCell class ");

    return  [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([cellClass class ]) forIndexPath:indexPath];
    
    return nil;
    
}
//添加刷新事件
-(void)collectionViewPullupHeaderRefresh:(void (^)(void))headerBlock
{
    __weak typeof(self)weakself=self;
    MJRefreshStateHeader  *headerView=[MJRefreshStateHeader  headerWithRefreshingBlock:^{
        if (headerBlock)
        {
            headerBlock();
        }
    }];
    weakself.collectionView.mj_header=headerView;
}
-(void)collectionViewPullDownFootRefresh:(void (^)(void))footerBlock
{
    __weak typeof(self)weakself=self;
    MJRefreshBackNormalFooter  *footrefreshView=[MJRefreshBackNormalFooter  footerWithRefreshingBlock:^{
        if (footerBlock)
        {
            footerBlock();
        }
    }];
    weakself.collectionView.mj_footer=footrefreshView;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kMainScreenWidth, 100);
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
{
    return 0;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}
//停止刷新
-(void)collectionViewViewEndRefresh
{
    [self.collectionView reloadData];
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView .mj_header endRefreshing];
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}
#pragma mark - 网络状态的实时检测；
- (void)congigNetWorkReachable
{
    //开启网络监视器；
    AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [afNetworkReachabilityManager startMonitoring];
    [afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
     {
         
         switch (status)
         {
             case AFNetworkReachabilityStatusNotReachable:
             {
                 
                 _NetWorkStatus=AFNetworkReachabilityStatusNotReachable;
                 //PPRSLog(@"网络不通：%f", rect.origin.y );
                 break;
             }
             case AFNetworkReachabilityStatusReachableViaWiFi:
             {
                 _NetWorkStatus=AFNetworkReachabilityStatusReachableViaWiFi;
                 
                 //PPRSLog(@"网络通过WIFI连接：%f", rect.origin.y );
                 break;
             }
             case AFNetworkReachabilityStatusReachableViaWWAN:
             {
                 _NetWorkStatus=AFNetworkReachabilityStatusReachableViaWWAN;
                 
                 // PPRSLog(@"网络通过无线连接：%f", rect.origin.y );
                 break;
             }
             default:
                 break;
         }
     }];
}
-(NSMutableArray *)dataListArray
{
    if (_dataListArray==nil)
    {
      _dataListArray=[[NSMutableArray alloc] init];
    }
    return _dataListArray;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
